package com.mcml.space;

import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;

public class DamageResulter implements Listener {
    @EventHandler
    public void DamageResulter(EntityDamageByEntityEvent event) {
        try {
            Entity tempattacker = event.getDamager();
            Entity tempbeenattacker = event.getEntity();
            if (tempattacker instanceof LivingEntity && tempbeenattacker instanceof LivingEntity) {

                LivingEntity attacker = (LivingEntity) tempattacker;
                LivingEntity entity = (LivingEntity) tempbeenattacker;
                ItemStack attackitem = null;
                ItemStack entityitem = null;
                EntityEquipment entityeq = entity.getEquipment();
                EntityEquipment attackereq = attacker.getEquipment();
                if (attacker instanceof Player) {
                    attackitem = ((Player) attacker).getItemInHand();
                }

                if (entity instanceof Player) {
                    entityitem = ((Player) entity).getItemInHand();
                }

                if (attackitem == null || ItemLores.isMotaRPGItem(attackitem)) {
                    int physicsAttack = 0;
                    int physicsProtect = 0;
                    int magicDamage = 0;
                    int magicProtect = 0;
                    int combieTimes = 0;
                    int critChance = 0;
                    boolean crit = false;
                    boolean strong = false;
                    if (entityitem != null) {
                        physicsProtect += ItemLores.getPhysicsProtect(entityitem);
                        magicProtect += ItemLores.getMagicProtect(entityitem);
                        if (!strong) {
                            strong = ItemLores.IsStrong(entityitem);
                        }
                    }

                    ItemStack[] ac;
                    int enddamage;
                    int endmagicProtect;
                    ItemStack thisitem;
                    if (entityeq != null) {
                        ac = entityeq.getArmorContents();
                        enddamage = ac.length;

                        for (endmagicProtect = 0; enddamage < endmagicProtect; ++endmagicProtect) {
                            thisitem = ac[endmagicProtect];
                            if (thisitem != null) {
                                magicProtect += ItemLores.getMagicProtect(thisitem);
                                physicsProtect += ItemLores.getPhysicsProtect(thisitem);
                                if (!strong) {
                                    strong = ItemLores.IsStrong(entityitem);
                                }
                            }
                        }
                    }

                    if (attackitem != null) {
                        if (!strong) {
                            physicsAttack = ItemLores.getPhysicsAttack(attackitem);
                        } else {
                            physicsAttack = 1;
                        }

                        magicDamage = ItemLores.getMagicAttack(attackitem);
                        combieTimes = ItemLores.getCombieTimes(attackitem);
                        critChance = ItemLores.getCritChance(attackitem);
                    }

                    if (attackereq != null) {
                        ac = entityeq.getArmorContents();
                        enddamage = ac.length;

                        for (endmagicProtect = 0; enddamage < endmagicProtect; ++endmagicProtect) {
                            thisitem = ac[endmagicProtect];
                            if (thisitem != null) {
                                if (!strong) {
                                    physicsAttack = ItemLores.getPhysicsAttack(attackitem);
                                } else {
                                    physicsAttack = 1;
                                }

                                magicDamage = ItemLores.getMagicAttack(attackitem);
                            }
                        }
                    }

                    int rn = Utils.randomNumber(100);
                    if (rn < critChance) {
                        crit = true;
                    }

                    int physicsdamage;
                    if (physicsAttack <= physicsProtect) {
                        physicsdamage = 0;
                    } else {
                        physicsdamage = physicsAttack - physicsProtect;
                    }

                    enddamage = (physicsdamage + magicDamage) * combieTimes;
                    endmagicProtect = magicProtect * combieTimes;
                    if (crit) {
                        enddamage *= 2;
                        attacker.getWorld().playEffect(entity.getEyeLocation(), Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
                    }

                    Utils.Heal(entity, (double) endmagicProtect);
                    if (attackitem != null) {
                        entity.damage((double) enddamage);
                        event.setDamage(0.0D);
                    } else if (strong) {
                        event.setDamage(1.0D);
                    } else {
                        event.setDamage(event.getDamage() - (double) physicsProtect);
                    }
                }
            }
        } catch (Exception e) {
        }
    }
}